﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using Swsk33.SimpleArcEngine.Dialog;
using Swsk33.SimpleArcEngine.Param;
using System;

namespace Swsk33.SimpleArcEngine.Util
{
	/// <summary>
	/// 画元素工具类
	/// </summary>
	public class DrawElement : ICommand, ITool
	{
		/// <summary>
		/// 被操作地图对象
		/// </summary>
		private AxMapControl map;

		/// <summary>
		/// 当前工具绘制的元素类型
		/// </summary>
		private ElementType elementType;

		/// <summary>
		/// 绘制图片时，图片的路径
		/// </summary>
		private string imagePath;

		/// <summary>
		/// 绘制工具类有参构造器
		/// </summary>
		/// <param name="elementType">要画的元素类型</param>
		public DrawElement(ElementType elementType)
		{
			this.elementType = elementType;
		}

		public void OnCreate(object Hook)
		{
			map = (AxMapControl)Hook;
		}

		public bool Enabled => true;

		public bool Checked => false;
		public string Name { get; set; }

		public string Caption { get; set; }

		public string Tooltip { get; set; }

		public string Message { get; set; }

		public string HelpFile { get; set; }

		public int HelpContextID { get; set; }

		public int Bitmap { get; set; }

		public string Category { get; set; }

		public int Cursor { get; set; }

		public void OnClick()
		{
			if (elementType == ElementType.IMAGE)
			{
				imagePath = new ImageDialog().ShowImageDialog();
			}
		}

		public void OnMouseDown(int button, int shift, int x, int y)
		{
			IGeometry geometry = null;
			IElement element = null;
			switch (elementType)
			{
				case ElementType.POINT:
					geometry = map.ToMapPoint(x, y);
					element = new MarkerElement();
					break;
				case ElementType.LINE:
					geometry = map.TrackLine();
					element = new LineElement();
					break;
				case ElementType.POLYGON:
					geometry = map.TrackPolygon();
					element = new PolygonElement();
					break;
				case ElementType.TEXT:
					geometry = map.ToMapPoint(x, y);
					element = new TextElement();
					string content = new TextDialog().ShowTextDialog();
					if (content.Equals(""))
					{
						return;
					}
					else
					{
						((ITextElement)element).Text = content;
					}
					break;
				case ElementType.IMAGE:
					geometry = map.TrackRectangle();
					if (imagePath == "")
					{
						return;
					}
					else
					{
						if (imagePath.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
						{
							element = new JpgPictureElement();
						}
						else if (imagePath.EndsWith("bmp", StringComparison.OrdinalIgnoreCase))
						{
							element = new BmpPictureElement();
						}
						else if (imagePath.EndsWith("png", StringComparison.OrdinalIgnoreCase))
						{
							element = new PngPictureElement();
						}
						else if (imagePath.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
						{
							element = new GifPictureElement();
						}
						((IPictureElement)element).ImportPictureFromFile(imagePath);
					}
					break;
			}
			// 设定坐标系
			element.Geometry = geometry;
			// 获取地图的图像容器
			IGraphicsContainer container = map.ActiveView.GraphicsContainer;
			// 加入至地图
			container.AddElement(element, 0);
			map.Refresh();
		}

		public void OnMouseMove(int button, int shift, int x, int y) { }

		public void OnMouseUp(int button, int shift, int x, int y) { }

		public void OnDblClick() { }

		public void OnKeyDown(int keyCode, int shift) { }

		public void OnKeyUp(int keyCode, int shift) { }

		public void Refresh(int hdc) { }

		public bool OnContextMenu(int x, int y)
		{
			return true;
		}

		public bool Deactivate()
		{
			return true;
		}
	}
}